<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_optimize extends omnitrix_controller
{
	public function index_action()
	{
		if (otx_request_is_post()) {
			$selected_tables = otx_param_array('tables');

			if ($selected_tables) {
				@set_time_limit(0);

				foreach ($selected_tables as $table_name) {
					$GLOBALS['_XDB']->query('OPTIMIZE TABLE ' . otx_db_escape_identifier($table_name));
					$GLOBALS['_XDB']->query('ANALYZE TABLE ' . otx_db_escape_identifier($table_name));
				}

				otx_admin_flash_message($this->_('text_success_optimize'));
				otx_admin_log_action($this->_('text_success_optimize'));
				otx_router_redirect('index');
			}

			otx_admin_flash_warning($this->_('error_tables_empty'));
		}

		$table_list = array();

		foreach ($GLOBALS['_XDB']->list_tables() as $table_name) {
			$table_list[$table_name] = $table_name;
		}

		$form_wrapper = otx_html_create_wrapper(null, null, $this->_('button_optimize'));
		$form_table = $form_wrapper->create_table($this->_('heading_index'), 'table-form');
		$form_table->add_form_entry($this->_('entry_tables'), otx_html_form_select('tables[]', null, array('size' => 15, 'multiple' => 'multiple', 'options' => $table_list)));

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_request_uri(), true);
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));
	}
}